
requirejs.config({
    baseUrl: '/js',
});

//doll.js requires signals and jquery to function
requirejs(['lib/signals.min', 'lib/jquery.min', 'engine/doll', 'engine/ui', 'engine/ui/selectable',
           'engine/draw', 'engine/fetch', 'engine/audio', 'engine/input', 'engine/cam',
          'engine/text'],
          function(signals) {
              //I barely know how I got this but it works
              //console.log = function () {};
              doll.signals = signals;
              var stats, render, camera, scene, clock;
              init();
              add_info();
              setup_audio();
              fill_scene();
              add_stats();
              animate();
         });

var cube;
function fill_scene() {
    cube = new THREE.Mesh(
        new THREE.CubeGeometry(50,50,50),
        new THREE.MeshLambertMaterial({color: 0xff00ff})
    );

    cube.position.set(0,75,0);

    var light = new THREE.SpotLight(0xffffff);
    light.position.set(1000,1000,1000);
    scene.add(light);

    for(var i=-4; i<4; i++) {
        scene.add(new doll.ui.selectable(new THREE.Vector3(i*100,25,0)));
    }

    scene.add(cube);

    scene.add(doll.debug_grid());
}

function add_stats() {
    stats = new Stats();
    stats.setMode(0); // 0: fps, 1: ms

    // Align top-left
    stats.domElement.style.position = 'absolute';
    stats.domElement.style.left = '0px';
    stats.domElement.style.top = '0px';
    document.body.appendChild( stats.domElement );
}

var container
function add_info() {
    container = document.createElement( 'div' );
    document.body.appendChild( container );
    
    var info = document.createElement( 'div' );
    info.style.position = 'absolute';
    info.style.top = '10px';
    info.style.width = '100%';
    info.style.textAlign = 'center';
    info.innerHTML = 'doll.js text test <br><strong> click to activate pointerlock </strong>';
    container.appendChild( info );
}

function init() {
    clock = new THREE.Clock();
    scene = new THREE.Scene();
    camera = new doll.cam( 45, window.innerWidth / window.innerHeight, 1, 10000 );
    camera.position.set(0, 300, 1000);
    render = new THREE.WebGLRenderer( {antialias: true} );
    render.setSize( window.innerWidth, window.innerHeight );

    var $view = $('#doll_view');
    $view.append(render.domElement);
    $('canvas').attr('id', '3d_canvas');
    doll.view = $view;
    //document.body.appendChild( render.domElement );

    //config engine
    //should be done with config function in the future
    doll.window = window;
    doll.document = document;
    doll.render = render;
    doll.camera = camera;//current camera global
    doll.scene = scene; //current scene global
    doll.window_width = window.innerWidth;
    doll.window_height = window.innerHeight;
    doll.halfwidth = doll.window_width * 0.5;
    doll.halfheight = doll.window_height * 0.5;

    //init modules order matters will write a nice
    doll.init();
    doll.text.init();//try out text here...

    doll.input.on_mouseenter(function(mouse) {
        console.log('Enter', mouse);
    });

    doll.input.on_mouseleave(function(mouse) {
        console.log('Leave', mouse);
    });

    //activate user controls
    camera.control();
}

function setup_audio() {
    //loads from assets/audio
    var audio = [
//        'ponponpon.wav',
//        'oniichan_09.ogg',
        'piano_sheet01.ogg'
    ];
    doll.audio.complete = function() {
        console.log('hey I got all my audio');
        //doll.audio.play(0);
    }
    doll.audio.fetch(audio, false);
}

var sample = 0.0;
function animate() {
    doll.fps(60); //lock fps at 60
    doll.tick = (new Date).getTime();

    doll.run = function() {
        var curTime = (new Date).getTime();
        if( curTime < doll.tick) {
            //its not time to step so just render as often as possible
            render.render(scene, camera);
            requestAnimationFrame(doll.run, render.domElement);
            return;
        }

        //prevent massive frame jumps when user leaves here

        stats.begin();
        //var delta = clock.getDelta();
        var delta = doll.FPS / 1000.0;//convert from ms to seconds passed

        if(sample > 5.0) {
            console.log('FPS: ', 1000.0 / doll.FPS, 'delta: ', delta * 1000);
            sample = 0.0;
        } else {
            sample += delta;
        }

        camera.update(delta, cube);
        //input after updating camera because update clear move values
        doll.input.update(delta);
        doll.ui.animate(delta);

        render.render(scene, camera);
        stats.end();
        //next step happens at a fixed time to smooth out the animations
        doll.tick += doll.FPS;
        requestAnimationFrame(doll.run, render.domElement);
    };

    doll.run();
}



